package lnk

import (
	"oa-rest/common"

	"github.com/gin-gonic/gin"
)

func queryUserLnkHandler(c *gin.Context) {
	var res common.Response

	username := c.GetString("username")
	if username == "" {
		res.Format(c, "username is null")
	}

	lnks, err := queryUserLnk(username)
	if err != nil {
		res.Format(c, err.Error())
		return
	}
	res.Data = lnks
	res.Format(c)
}

// 获取所有快捷操作卡片
func getAllLnkHandler(c *gin.Context) {
	var res common.Response
	
	lnks, err := getAllLnk()
	if err != nil {
		res.Format(c, err.Error())
		return
	}
	
	res.Data = lnks
	res.Format(c)
}

// 更新用户快捷操作排序
func updateUserLnkOrderHandler(c *gin.Context) {
	var res common.Response
	var req struct {
		LnkIds []int64 `json:"lnkIds"`
	}
	
	if err := c.ShouldBindJSON(&req); err != nil {
		res.Format(c, "参数错误")
		return
	}
	
	username := c.GetString("username")
	if username == "" {
		res.Format(c, "username is null")
		return
	}
	
	if err := updateUserLnkOrder(username, req.LnkIds); err != nil {
		res.Format(c, err.Error())
		return
	}
	
	res.Format(c)
}

func RegisterRoutes(group *gin.RouterGroup) {
	group.POST("/lnk/queryUserLnk", queryUserLnkHandler)
	group.GET("/lnk/getAllLnk", getAllLnkHandler)           // 新增
	group.POST("/lnk/updateUserLnkOrder", updateUserLnkOrderHandler) // 新增
}
